package www.dianbaozu.com.ui.server.findMember

import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.viewModels
import com.luck.picture.lib.decoration.WrapContentLinearLayoutManager
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.api.SessionHelper
import com.netease.nim.uikit.common.ui.recyclerview.adapter.IRecyclerView
import com.netease.nim.uikit.common.ui.recyclerview.listener.SimpleClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.FlowPreview
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentFindMemberBinding
import www.dianbaozu.com.ui.server.findMember.adapter.FindMemberAdapter
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.ServiceMemberEntity
import www.wen.com.common.lib.extension.banEnterKey
import www.wen.com.common.lib.extension.finishMyself
import www.wen.com.common.lib.extension.navBack
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

@AndroidEntryPoint
class FindMemberFragment : BaseLoadingFragment<FragmentFindMemberBinding>(), View.OnClickListener {
    private val serverId by lazy { requireActivity().intent.getStringExtra("serverId") ?: "" }

    private val findMemberAdapter: FindMemberAdapter by lazy {
        FindMemberAdapter(
            dataBinding.rvMember,
            R.layout.item_friends,
            mutableListOf()
        )
    }

    private val findMemberViewModel: FindMemberViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = findMemberViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
        TitleBarUniversalAdapter(requireActivity(), "成员", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_find_member

    override fun lazyInitView() {
        dataBinding.apply {
            etSearch.addTextChangedListener {
                findMemberAdapter.clearData()
                findMemberAdapter.removeAllFooterView()
                findMemberAdapter.removeAllHeaderView()
                findMemberViewModel.page = 0
                searchServerMemberList(serverId, it.toString().trim())
            }
            etSearch.banEnterKey()
            rvMember.adapter = findMemberAdapter
            rvMember.layoutManager = WrapContentLinearLayoutManager(requireActivity())
            rvMember.addOnItemTouchListener(object : SimpleClickListener<IRecyclerView>(){
                override fun onItemClick(adapter: IRecyclerView?, view: View?, position: Int) {
                }

                override fun onItemLongClick(adapter: IRecyclerView?, view: View?, position: Int) {
                }

                override fun onItemChildClick(adapter: IRecyclerView?, view: View?, position: Int) {
                    val item = findMemberAdapter.data[position]
                    if (item is ServiceMemberEntity) {
                        if (item.userId == NIMHelper.getUserAccount()) {
                            return
                        }
                        SessionHelper.showBottomDialog(
                            requireActivity(),
                            item.userId,
                            item.allowFriendFlag == "1",
                            item.onlineStatus
                        ) { view, _, _ ->
                            if (view.id == R.id.llTwo) {
                                finishMyself()
                            }
                        }
                    }
                }

                override fun onItemChildLongClick(
                    adapter: IRecyclerView?,
                    view: View?,
                    position: Int
                ) {
                }
            })
        }
    }

    override fun onClick(v: View) {
        navBack()
    }

    @FlowPreview
    fun searchServerMemberList(serverId: String, nickName: String) {
        if (nickName.isEmpty()) {
            return
        }
        findMemberViewModel.searchServerMemberList(serverId, nickName) {
            findMemberAdapter.addData(it.data.content)
        }
    }

}



